/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.dialogs;

import fvm.FVElement;
import java.awt.Color;
import java.util.ArrayList;

/**
 *
 * @author bittens
 */
public class Dialog_Prop extends javax.swing.JDialog {

    static ArrayList<FVElement> alfve = null;
    boolean change_noflow = false;
    double kxx, kyy, kxy;
    double s0,n0;

    /**
     * Creates new form Dialog_Prop
     */
    public Dialog_Prop(java.awt.Frame parent, boolean modal, ArrayList<FVElement> alfve) {
        super(parent, modal);
        initComponents();
        this.alfve = alfve;
        initK();
        this.setVisible(true);









    }

    private void initK() {

        boolean bkxx = true, bkyy = true, bkxy = true, bnf = true, nf = alfve.get(0).isnoflow();
        boolean bn0 = true, bs0 = true;
        kxx = alfve.get(0).getperm().getKxx();
        kyy = alfve.get(0).getperm().getKyy();
        kxy = alfve.get(0).getperm().getKxy();
        s0  = alfve.get(0).getS0();
        n0  = alfve.get(0).getne();


        for (int i = 1; i < alfve.size(); i++) {

            if (kxx != alfve.get(i).getperm().getKxx()) {
                bkxx = false;
            }
            if (kyy != alfve.get(i).getperm().getKyy()) {
                bkyy = false;
            }
            if (kxy != alfve.get(i).getperm().getKxy()) {
                bkxy = false;
            }
            if (nf != alfve.get(i).isnoflow()) {
                bnf = false;
            }
            
            if (n0 != alfve.get(i).getne()) {
                bn0 = false;
            }
            
            if (s0 != alfve.get(i).getS0()) {
                bs0 = false;
            }
            

            if (!bkxx && !bkyy && !bkxy && !bn0 && !bs0) {
                break;
            }

        }

        if (bkxx) {
            jTF_Kxx.setText(Double.toString(alfve.get(0).getperm().getKxx()));
        } else {
            jTF_Kxx.setText("var");
        }
        if (bkyy) {
            jTF_Kyy.setText(Double.toString(alfve.get(0).getperm().getKyy()));
        } else {
            jTF_Kyy.setText("var");
        }
        if (bkxy) {
            jTF_Kxy.setText(Double.toString(alfve.get(0).getperm().getKxy()));
            // jTF_Kyx.setText(Double.toString(alfve.get(0).getperm().getKxy()));
        } else {

            jTF_Kxy.setText("var");
            //jTF_Kyx.setText("var");
        }
        if (bn0) {
            jTF_ne.setText(Double.toString(alfve.get(0).getne()));
        } else {
            jTF_ne.setText("var");
        }
        if (bs0) {
            jTF_S0.setText(Double.toString(alfve.get(0).getS0()));
        } else {
            jTF_S0.setText("var");
        }

        if (bnf) {

            jCB_noflow.setForeground(Color.black);
            jCB_noflow.setSelected(nf);
        }

        if (bnf && nf) {

            jTF_Kxx.setEnabled(false);
            jTF_Kxy.setEnabled(false);
            //jTF_Kyx.setEnabled(false);
            jTF_Kyy.setEnabled(false);
            jTF_S0.setEnabled(false);
            jTF_ne.setEnabled(false);

        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jB_OK = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTF_Kxx = new javax.swing.JTextPane();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTF_Kxy = new javax.swing.JTextPane();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTF_Kyy = new javax.swing.JTextPane();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane6 = new javax.swing.JScrollPane();
        jTF_ne = new javax.swing.JTextPane();
        jScrollPane7 = new javax.swing.JScrollPane();
        jTF_S0 = new javax.swing.JTextPane();
        jCB_noflow = new javax.swing.JCheckBox();

        jScrollPane1.setViewportView(jTextPane1);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jB_OK.setText("Ok");
        jB_OK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jB_OKActionPerformed(evt);
            }
        });

        jLabel1.setText("Kxx");

        jLabel3.setText("Kxy");
        jLabel3.setEnabled(false);

        jLabel4.setText("Kyy");

        jScrollPane2.setViewportView(jTF_Kxx);

        jScrollPane4.setEnabled(false);
        jScrollPane4.setViewportView(jTF_Kxy);

        jScrollPane5.setViewportView(jTF_Kyy);

        jLabel5.setText("ne");

        jLabel6.setText("S0");

        jScrollPane6.setViewportView(jTF_ne);

        jScrollPane7.setViewportView(jTF_S0);

        jCB_noflow.setForeground(new java.awt.Color(102, 102, 102));
        jCB_noflow.setText("no flow");
        jCB_noflow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCB_noflowActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(102, 102, 102)
                        .addComponent(jB_OK))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(24, 24, 24))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addGap(18, 18, 18)))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                                    .addComponent(jScrollPane6))))
                        .addGap(55, 55, 55)
                        .addComponent(jCB_noflow)))
                .addContainerGap(84, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCB_noflow)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane7, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                .addComponent(jB_OK)
                .addGap(19, 19, 19))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jCB_noflowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCB_noflowActionPerformed

        change_noflow = true;
        jCB_noflow.setForeground(Color.black);

        if (jCB_noflow.isSelected()) {

            jTF_Kxx.setEnabled(false);
            jTF_Kxy.setEnabled(false);
            //jTF_Kyx.setEnabled(false);
            jTF_Kyy.setEnabled(false);
            jTF_S0.setEnabled(false);
            jTF_ne.setEnabled(false);

        } else {

            jTF_Kxx.setEnabled(true);
            jTF_Kxy.setEnabled(true);
            //jTF_Kyx.setEnabled(true);
            jTF_Kyy.setEnabled(true);
            jTF_S0.setEnabled(true);
            jTF_ne.setEnabled(true);




        }

    }//GEN-LAST:event_jCB_noflowActionPerformed

    private void jB_OKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jB_OKActionPerformed

        double newkxy = Double.parseDouble(jTF_Kxy.getText());
        boolean chkxy = false;

        double newkxx = Double.parseDouble(jTF_Kxx.getText());
        boolean chkxx = false;

        double newkyy = Double.parseDouble(jTF_Kyy.getText());
        boolean chkyy = false;

        double newne = Double.parseDouble(jTF_ne.getText());
        boolean chne = false;

        double news0 = Double.parseDouble(jTF_S0.getText());
        boolean chs0 = false;

        

        if (newkxy != kxy) {

            chkxy = true;

        }

        if (newkxx != kxx) {

            chkxx = true;

        }
        if (newkyy != kyy) {

            chkyy = true;

        }
        if (newne != n0) {

            chne = true;

        }
        if (news0 != s0) {

            chs0 = true;

        }




        for (int i = 0; i < alfve.size(); i++) {
            
            
            FVElement fve = alfve.get(i);
            
            if(chkxy){
            
                fve.getperm().setKxy(newkxy);

            }

            if (chkxx) {

                fve.getperm().setKxx(newkxx);

            }

            if (chkyy) {

                fve.getperm().setKyy(newkyy);

            }

            if (chne) {

                fve.setne(newne);

            }

            if (chs0) {

                fve.setS0(news0);

            }
            
            if (change_noflow) {
                if (jCB_noflow.isSelected()) {
                    
                    fve.setnoflow(true);

                } else {

                    fve.setnoflow(false);

                }
            }
        }


        super.dispose();

    }//GEN-LAST:event_jB_OKActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Dialog_Prop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Dialog_Prop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Dialog_Prop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Dialog_Prop.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the dialog
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                Dialog_Prop dialog = new Dialog_Prop(new javax.swing.JFrame(), true, alfve);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jB_OK;
    private javax.swing.JCheckBox jCB_noflow;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JScrollPane jScrollPane7;
    private javax.swing.JTextPane jTF_Kxx;
    private javax.swing.JTextPane jTF_Kxy;
    private javax.swing.JTextPane jTF_Kyy;
    private javax.swing.JTextPane jTF_S0;
    private javax.swing.JTextPane jTF_ne;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
}
